<!DOCTYPE html>
<html lang=zh>
<head><meta name="generator" content="Hexo 3.9.0">
    <!-- so meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="HandheldFriendly" content="True">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="前言之前写过一篇文章–如何使用 travis 为 nodejs 持续集成，主要是用来在github上自动化部署自己的博客。今天来继续来讲travis如何和自己的服务器取得联系，自动去服务器上执行shell命令来完成部署。 服务器配置创建 travis 管理员账号123456#新建用户useradd travis#修改密码（应该不是必要，但是万一以后需要用密码登陆呢）,按照提示设置密码。passwd">
<meta name="keywords" content="nodejs,travis">
<meta property="og:type" content="article">
<meta property="og:title" content="travis自动化部署续篇">
<meta property="og:url" content="https://andyliwr.github.io/2018/04/28/travis_node_publish2/index.html">
<meta property="og:site_name" content="月光倾城的代码小屋">
<meta property="og:description" content="前言之前写过一篇文章–如何使用 travis 为 nodejs 持续集成，主要是用来在github上自动化部署自己的博客。今天来继续来讲travis如何和自己的服务器取得联系，自动去服务器上执行shell命令来完成部署。 服务器配置创建 travis 管理员账号123456#新建用户useradd travis#修改密码（应该不是必要，但是万一以后需要用密码登陆呢）,按照提示设置密码。passwd">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2020-01-15T06:30:09.120Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="travis自动化部署续篇">
<meta name="twitter:description" content="前言之前写过一篇文章–如何使用 travis 为 nodejs 持续集成，主要是用来在github上自动化部署自己的博客。今天来继续来讲travis如何和自己的服务器取得联系，自动去服务器上执行shell命令来完成部署。 服务器配置创建 travis 管理员账号123456#新建用户useradd travis#修改密码（应该不是必要，但是万一以后需要用密码登陆呢）,按照提示设置密码。passwd">
    
    
        
          
              <link rel="shortcut icon" href="/images/favicon.ico">
          
        
        
          
            <link rel="icon" type="image/png" href="/images/favicon-192x192.png" sizes="192x192">
          
        
        
          
            <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
          
        
    
    <!-- title -->
    <title>travis自动化部署续篇</title>
    <!-- styles -->
    <link rel="stylesheet" href="/css/style.css">
    <!-- persian styles -->
    
      <link rel="stylesheet" href="/css/rtl.css">
    
    <!-- rss -->
    
    
</head>

<body class="max-width mx-auto px3 ltr">
    
      <div id="header-post">
  <a id="menu-icon" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="menu-icon-tablet" href="#"><i class="fas fa-bars fa-lg"></i></a>
  <a id="top-icon-tablet" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');" style="display:none;"><i class="fas fa-chevron-up fa-lg"></i></a>
  <span id="menu">
    <span id="nav">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </span>
    <br/>
    <span id="actions">
      <ul>
        
        <li><a class="icon" href="/2018/05/03/how_to_write_a_timed_shell/"><i class="fas fa-chevron-left" aria-hidden="true" onmouseover="$('#i-prev').toggle();" onmouseout="$('#i-prev').toggle();"></i></a></li>
        
        
        <li><a class="icon" href="/2018/04/24/node_gpy_install_fail/"><i class="fas fa-chevron-right" aria-hidden="true" onmouseover="$('#i-next').toggle();" onmouseout="$('#i-next').toggle();"></i></a></li>
        
        <li><a class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up" aria-hidden="true" onmouseover="$('#i-top').toggle();" onmouseout="$('#i-top').toggle();"></i></a></li>
        <li><a class="icon" href="#"><i class="fas fa-share-alt" aria-hidden="true" onmouseover="$('#i-share').toggle();" onmouseout="$('#i-share').toggle();" onclick="$('#share').toggle();return false;"></i></a></li>
      </ul>
      <span id="i-prev" class="info" style="display:none;">上一篇</span>
      <span id="i-next" class="info" style="display:none;">下一篇</span>
      <span id="i-top" class="info" style="display:none;">返回顶部</span>
      <span id="i-share" class="info" style="display:none;">分享文章</span>
    </span>
    <br/>
    <div id="share" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2018/04/28/travis_node_publish2/"><i class="fab fa-facebook " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&text=travis自动化部署续篇"><i class="fab fa-twitter " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&title=travis自动化部署续篇"><i class="fab fa-linkedin " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&is_video=false&description=travis自动化部署续篇"><i class="fab fa-pinterest " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=travis自动化部署续篇&body=Check out this article: https://andyliwr.github.io/2018/04/28/travis_node_publish2/"><i class="fas fa-envelope " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&title=travis自动化部署续篇"><i class="fab fa-get-pocket " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&title=travis自动化部署续篇"><i class="fab fa-reddit " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&title=travis自动化部署续篇"><i class="fab fa-stumbleupon " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&title=travis自动化部署续篇"><i class="fab fa-digg " aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&name=travis自动化部署续篇&description="><i class="fab fa-tumblr " aria-hidden="true"></i></a></li>
</ul>

    </div>
    <div id="toc">
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#服务器配置"><span class="toc-number">2.</span> <span class="toc-text">服务器配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#创建-travis-管理员账号"><span class="toc-number">2.1.</span> <span class="toc-text">创建 travis 管理员账号</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#生成部署秘钥"><span class="toc-number">2.2.</span> <span class="toc-text">生成部署秘钥</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#添加公钥到受信列表中"><span class="toc-number">3.</span> <span class="toc-text">添加公钥到受信列表中</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#测试-ssh-登录"><span class="toc-number">4.</span> <span class="toc-text">测试 ssh 登录</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#安装-travis-命令行工具"><span class="toc-number">5.</span> <span class="toc-text">安装 travis 命令行工具</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#登录-travis-并生成部署的秘钥"><span class="toc-number">6.</span> <span class="toc-text">登录 travis 并生成部署的秘钥</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#编写登录服务器之后的代码"><span class="toc-number">7.</span> <span class="toc-text">编写登录服务器之后的代码</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-复制本地文件到远程服务器"><span class="toc-number">7.1.</span> <span class="toc-text">1. 复制本地文件到远程服务器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-登录远程服务器并执行写好的-shell-脚本"><span class="toc-number">7.2.</span> <span class="toc-text">2. 登录远程服务器并执行写好的 shell 脚本</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#结束"><span class="toc-number">8.</span> <span class="toc-text">结束</span></a></li></ol>
    </div>
  </span>
</div>

    
    <div class="content index py4">
        
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
  <header>
    
    <h1 class="posttitle" itemprop="name headline">
        travis自动化部署续篇
    </h1>



    <div class="meta">
      <span class="author" itemprop="author" itemscope itemtype="http://schema.org/Person">
        <span itemprop="name">月光倾城的代码小屋</span>
      </span>
      
    <div class="postdate">
        <time datetime="2018-04-28T15:48:56.000Z" itemprop="datePublished">2018-04-28</time>
    </div>


      

      
    <div class="article-tag">
        <i class="fas fa-tag"></i>
        <a class="tag-link" href="/tags/nodejs/">nodejs</a>, <a class="tag-link" href="/tags/travis/">travis</a>
    </div>


    </div>
  </header>
  

  <div class="content" itemprop="articleBody">
    <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p>之前写过一篇文章–<a href="https://andyliwr.github.io/2017/12/05/travis_nodejs_publish/">如何使用 travis 为 nodejs 持续集成</a>，主要是用来在<code>github</code>上自动化部署自己的博客。今天来继续来讲<code>travis</code>如何和自己的服务器取得联系，自动去服务器上执行<code>shell</code>命令来完成部署。</p>
<h3 id="服务器配置"><a href="#服务器配置" class="headerlink" title="服务器配置"></a>服务器配置</h3><h4 id="创建-travis-管理员账号"><a href="#创建-travis-管理员账号" class="headerlink" title="创建 travis 管理员账号"></a>创建 travis 管理员账号</h4><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#新建用户</span></span><br><span class="line"><span class="attr">useradd</span> <span class="string">travis</span></span><br><span class="line"><span class="comment">#修改密码（应该不是必要，但是万一以后需要用密码登陆呢）,按照提示设置密码。</span></span><br><span class="line"><span class="attr">passwd</span> <span class="string">travis</span></span><br><span class="line"><span class="comment">#为用户添加添加权限</span></span><br><span class="line"><span class="attr">sudo</span> <span class="string">vim /etc/sudoers</span></span><br></pre></td></tr></table></figure>
<p>找到<code>#Allow root to run any commands anywhere</code>这一行，在下方添加<code>travis ALL=(ALL:ALL) ALL</code><br>编辑权限文件的时候如果出现保存不了的问题，可以输入<code>:wq!</code>强制保存</p>
<h4 id="生成部署秘钥"><a href="#生成部署秘钥" class="headerlink" title="生成部署秘钥"></a>生成部署秘钥</h4><figure class="highlight properties"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">su</span> <span class="string">travis</span></span><br><span class="line"><span class="attr">sudo</span> <span class="string">ssh-keygen -t rsa</span></span><br></pre></td></tr></table></figure>
<p>生秘钥的时候不要设置<code>passphase</code>，留空就好如果 ssh-keygen 命令不存在，请先安装<code>openssh-client</code>。</p>
<figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">sudo apt-<span class="builtin-name">get</span> install ssh</span><br></pre></td></tr></table></figure>
<p>如果出现不存在/home/travis 的目录，则需要手动添加</p>
<figure class="highlight arduino"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">sudo <span class="built_in">mkdir</span> /<span class="built_in">home</span>/travis</span><br><span class="line">sudo chown travis:travis /<span class="built_in">home</span>/travis</span><br></pre></td></tr></table></figure>
<p>然后修改<code>.ssh</code>目录权限</p>
<figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#设置.ssh目录为700</span></span><br><span class="line">chmod 700 ~<span class="string">/.ssh</span></span><br><span class="line"><span class="comment">#设置.ssh目录下的文件为600</span></span><br><span class="line">chmod 600 ~<span class="string">/.ssh/</span>*</span><br><span class="line"><span class="keyword">ls</span> -al</span><br><span class="line"><span class="keyword">ls</span> ~<span class="string">/.ssh/</span> -al</span><br></pre></td></tr></table></figure>
<h3 id="添加公钥到受信列表中"><a href="#添加公钥到受信列表中" class="headerlink" title="添加公钥到受信列表中"></a>添加公钥到受信列表中</h3><figure class="highlight matlab"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">cat</span> id_rsa.pub &gt;&gt; authorized_keys</span><br><span class="line"><span class="built_in">cat</span> authorized_keys</span><br></pre></td></tr></table></figure>
<h3 id="测试-ssh-登录"><a href="#测试-ssh-登录" class="headerlink" title="测试 ssh 登录"></a>测试 ssh 登录</h3><figure class="highlight routeros"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">vi config</span><br><span class="line"><span class="comment"># 写入以下内容</span></span><br><span class="line">------------</span><br><span class="line">Host khweb</span><br><span class="line">HostName 99.99.99.99(你的服务器ip)</span><br><span class="line"><span class="comment">#登陆的用户名</span></span><br><span class="line">User travis</span><br><span class="line">IdentitiesOnly <span class="literal">yes</span></span><br><span class="line"><span class="comment">#登陆使用的密钥</span></span><br><span class="line">IdentityFile ~/.ssh/id_rsa</span><br><span class="line">-------------</span><br><span class="line"></span><br><span class="line">ssh test</span><br><span class="line"><span class="comment"># 输出 Bad owner or permissions on /home/travis/.ssh/config</span></span><br><span class="line"><span class="comment"># 注意此时的测试是失败的，因为authorized_keys和config是我们后面添加的文件，文件权限并不是600</span></span><br><span class="line"></span><br><span class="line">ls -al</span><br><span class="line">chmod 600 config</span><br><span class="line">chmod 600 authorized_keys</span><br><span class="line">ls -al</span><br><span class="line">ssh test</span><br><span class="line"><span class="comment"># 测试成功，生成了一个known_hosts文件，以后再登陆时就不需要在输入yes确认了，你可以再做一次测试</span></span><br></pre></td></tr></table></figure>
<h3 id="安装-travis-命令行工具"><a href="#安装-travis-命令行工具" class="headerlink" title="安装 travis 命令行工具"></a>安装 travis 命令行工具</h3><figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 安装rvm</span></span><br><span class="line">gpg <span class="params">--keyserver</span> hkp:<span class="string">//keys.gnupg.net</span> <span class="params">--recv-keys</span> 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB</span><br><span class="line">curl -sSL https:<span class="string">//get.rvm.io</span> | bash -s stable</span><br><span class="line">source <span class="string">/home/travis/.rvm/scripts/rvm</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 安装ruby</span></span><br><span class="line">rvm install ruby</span><br><span class="line">gem -v</span><br><span class="line">gem sources <span class="params">--add</span> https:<span class="string">//gems.ruby-china.org/</span> <span class="params">--remove</span> https:<span class="string">//rubygems.org/</span></span><br><span class="line">gem sources -l</span><br><span class="line"></span><br><span class="line"><span class="comment"># 安装travis</span></span><br><span class="line">gem install travis</span><br></pre></td></tr></table></figure>
<h3 id="登录-travis-并生成部署的秘钥"><a href="#登录-travis-并生成部署的秘钥" class="headerlink" title="登录 travis 并生成部署的秘钥"></a>登录 travis 并生成部署的秘钥</h3><figure class="highlight livecodeserver"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">travis login</span><br><span class="line">travis <span class="built_in">encrypt</span>-<span class="built_in">file</span> ~/.ssh/id_rsa <span class="comment">--add</span></span><br></pre></td></tr></table></figure>
<p>查看下<code>.travis.yml</code>就可以看到自动添加的改动</p>
<figure class="highlight diff"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="addition">+ before_install:</span></span><br><span class="line"><span class="addition">+  - openssl aes-256-cbc -K $encrypted_20c4b952970b_key -iv</span></span><br><span class="line"><span class="addition">+    $encrypted_20c4b952970b_iv -in id_rsa.enc -out ~\/.ssh/id_rsa -d</span></span><br></pre></td></tr></table></figure>
<p>这里我们改下，将<code>id_rsa.enc</code>移动到一个名为<code>.travis</code>的文件夹下，<code>~\/.ssh/id_rsa</code>也经常找不到，所以改成<code>~/.ssh/id_rsa</code>。最后就变成这样：</p>
<figure class="highlight jboss-cli"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">mkdir <span class="string">.travis</span> &amp;&amp; mv id_rsa.enc <span class="string">.travis/</span></span><br></pre></td></tr></table></figure>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">before_install:</span></span><br><span class="line"><span class="bullet">-</span> <span class="string">openssl</span> <span class="string">aes-256-cbc</span> <span class="string">-K</span> <span class="string">$encrypted_20c4b952970b_key</span> <span class="string">-iv</span> <span class="string">$encrypted_20c4b952970b_iv</span></span><br><span class="line">  <span class="string">-in</span> <span class="string">.travis/id_rsa.enc</span> <span class="string">-out</span> <span class="string">~/.ssh/id_rsa</span> <span class="string">-d</span></span><br></pre></td></tr></table></figure>
<h3 id="编写登录服务器之后的代码"><a href="#编写登录服务器之后的代码" class="headerlink" title="编写登录服务器之后的代码"></a>编写登录服务器之后的代码</h3><h4 id="1-复制本地文件到远程服务器"><a href="#1-复制本地文件到远程服务器" class="headerlink" title="1. 复制本地文件到远程服务器"></a>1. 复制本地文件到远程服务器</h4><p>比如一个前端项目，在<code>npm run build</code>之后需要把打包的<code>dist</code>传到服务器的静态资源目录完成部署，这个时候就可以使用<a href="http://www.runoob.com/linux/linux-comm-scp.html" target="_blank" rel="noopener"><code>scp</code></a>命令：</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">after_success:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">chmod</span> <span class="number">600</span> <span class="string">~/.ssh/id_rsa</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">scp</span> <span class="string">-r</span> <span class="string">dist/</span> <span class="string">travis@193.112.196.41:/var/www/admin</span></span><br></pre></td></tr></table></figure>
<h4 id="2-登录远程服务器并执行写好的-shell-脚本"><a href="#2-登录远程服务器并执行写好的-shell-脚本" class="headerlink" title="2. 登录远程服务器并执行写好的 shell 脚本"></a>2. 登录远程服务器并执行写好的 shell 脚本</h4><p>在 travis 目录下新建一个<code>deploy.sh</code>，执行如下命令：</p>
<figure class="highlight vim"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">cd</span> /home/ubuntu/mbook-koa</span><br><span class="line"><span class="keyword">echo</span> <span class="string">"从远程拉取代码"</span></span><br><span class="line">git pull</span><br><span class="line"><span class="keyword">echo</span> <span class="string">"最新改动如下"</span></span><br><span class="line">git diff HEAD^</span><br><span class="line"><span class="keyword">echo</span> <span class="string">"PM2 进程"</span></span><br><span class="line"><span class="keyword">source</span> /home/ubuntu/.nvm/nvm.<span class="keyword">sh</span></span><br><span class="line">pm2 <span class="keyword">list</span></span><br><span class="line"><span class="keyword">echo</span> <span class="string">"重启mbook-koa"</span></span><br><span class="line">pm2 restart mbook-koa</span><br></pre></td></tr></table></figure>
<p><code>.travis.yml</code>内容如下：</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">after_success:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">chmod</span> <span class="number">600</span> <span class="string">~/.ssh/id_rsa</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">ssh</span> <span class="string">travis@193.112.196.41</span> <span class="string">"/home/ubuntu/mbook-koa/.travis/deploy.sh"</span></span><br></pre></td></tr></table></figure>
<h3 id="结束"><a href="#结束" class="headerlink" title="结束"></a>结束</h3><p>最后贴个自己写好的<code>.travis.yml</code>，祝您编码愉快!</p>
<figure class="highlight yml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">language:</span> <span class="string">node_js</span></span><br><span class="line"><span class="attr">node_js:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">'9'</span></span><br><span class="line"><span class="attr">branchs:</span></span><br><span class="line">  <span class="attr">only:</span></span><br><span class="line">  <span class="bullet">-</span> <span class="string">master</span></span><br><span class="line"><span class="attr">cache:</span></span><br><span class="line">  <span class="attr">directories:</span></span><br><span class="line">    <span class="bullet">-</span> <span class="string">"node_modules"</span></span><br><span class="line"><span class="attr">install:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">npm</span> <span class="string">install</span></span><br><span class="line"><span class="attr">scripts:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">npm</span> <span class="string">run</span> <span class="string">test</span></span><br><span class="line"><span class="attr">addons:</span></span><br><span class="line">  <span class="attr">ssh_known_hosts:</span></span><br><span class="line">   <span class="bullet">-</span> <span class="number">193.112</span><span class="number">.196</span><span class="number">.41</span></span><br><span class="line"><span class="attr">before_install:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">openssl</span> <span class="string">aes-256-cbc</span> <span class="string">-K</span> <span class="string">$encrypted_c2df1cea7f22_key</span> <span class="string">-iv</span> <span class="string">$encrypted_c2df1cea7f22_iv</span></span><br><span class="line">   <span class="string">-in</span> <span class="string">.travis/id_rsa.enc</span> <span class="string">-out</span> <span class="string">~/.ssh/id_rsa</span> <span class="string">-d</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">sudo</span> <span class="string">apt-get</span> <span class="string">update</span> <span class="string">-q</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">sudo</span> <span class="string">apt-get</span> <span class="string">install</span> <span class="string">libcairo2-dev</span> <span class="string">libjpeg-dev</span> <span class="string">libpango1.0-dev</span> <span class="string">libgif-dev</span> <span class="string">build-essential</span> <span class="string">g++</span></span><br><span class="line"><span class="attr">after_success:</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">chmod</span> <span class="number">600</span> <span class="string">~/.ssh/id_rsa</span></span><br><span class="line"> <span class="bullet">-</span> <span class="string">ssh</span> <span class="string">travis@193.112.196.41</span> <span class="string">"/home/ubuntu/mbook-koa/.travis/deploy.sh"</span></span><br></pre></td></tr></table></figure>

  </div>
  <div class="statement">
    <hr>  
    1. 如果本文帮到了您，不妨点一下右上角的”<i class="fas fa-share-alt"></i>“按钮。<br>
    2. 除非注明，本博文章均为原创，转载请以链接形式标明本文地址。<br>
    3. 如果你有任何疑问，欢迎发送邮件到我的邮箱<a href="mailto:andyliwr@outlook.com">andyliwr@outlook.com<br>
  </div>
</article>


    <div class="blog-post-comments">
        <div id="gitalk_thread">
            <noscript>加载评论需要在浏览器启用 JavaScript 脚本支持。</noscript>
        </div>
    </div>



        
          <div id="footer-post-container">
  <div id="footer-post">

    <div id="nav-footer" style="display: none">
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </div>

    <div id="toc-footer" style="display: none">
      <ol class="toc"><li class="toc-item toc-level-3"><a class="toc-link" href="#前言"><span class="toc-number">1.</span> <span class="toc-text">前言</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#服务器配置"><span class="toc-number">2.</span> <span class="toc-text">服务器配置</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#创建-travis-管理员账号"><span class="toc-number">2.1.</span> <span class="toc-text">创建 travis 管理员账号</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#生成部署秘钥"><span class="toc-number">2.2.</span> <span class="toc-text">生成部署秘钥</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#添加公钥到受信列表中"><span class="toc-number">3.</span> <span class="toc-text">添加公钥到受信列表中</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#测试-ssh-登录"><span class="toc-number">4.</span> <span class="toc-text">测试 ssh 登录</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#安装-travis-命令行工具"><span class="toc-number">5.</span> <span class="toc-text">安装 travis 命令行工具</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#登录-travis-并生成部署的秘钥"><span class="toc-number">6.</span> <span class="toc-text">登录 travis 并生成部署的秘钥</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#编写登录服务器之后的代码"><span class="toc-number">7.</span> <span class="toc-text">编写登录服务器之后的代码</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-复制本地文件到远程服务器"><span class="toc-number">7.1.</span> <span class="toc-text">1. 复制本地文件到远程服务器</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-登录远程服务器并执行写好的-shell-脚本"><span class="toc-number">7.2.</span> <span class="toc-text">2. 登录远程服务器并执行写好的 shell 脚本</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#结束"><span class="toc-number">8.</span> <span class="toc-text">结束</span></a></li></ol>
    </div>

    <div id="share-footer" style="display: none">
      <ul>
  <li><a class="icon" href="http://www.facebook.com/sharer.php?u=https://andyliwr.github.io/2018/04/28/travis_node_publish2/"><i class="fab fa-facebook fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://twitter.com/share?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&text=travis自动化部署续篇"><i class="fab fa-twitter fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.linkedin.com/shareArticle?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&title=travis自动化部署续篇"><i class="fab fa-linkedin fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://pinterest.com/pin/create/bookmarklet/?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&is_video=false&description=travis自动化部署续篇"><i class="fab fa-pinterest fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="mailto:?subject=travis自动化部署续篇&body=Check out this article: https://andyliwr.github.io/2018/04/28/travis_node_publish2/"><i class="fas fa-envelope fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="https://getpocket.com/save?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&title=travis自动化部署续篇"><i class="fab fa-get-pocket fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://reddit.com/submit?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&title=travis自动化部署续篇"><i class="fab fa-reddit fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.stumbleupon.com/submit?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&title=travis自动化部署续篇"><i class="fab fa-stumbleupon fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://digg.com/submit?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&title=travis自动化部署续篇"><i class="fab fa-digg fa-lg" aria-hidden="true"></i></a></li>
  <li><a class="icon" href="http://www.tumblr.com/share/link?url=https://andyliwr.github.io/2018/04/28/travis_node_publish2/&name=travis自动化部署续篇&description="><i class="fab fa-tumblr fa-lg" aria-hidden="true"></i></a></li>
</ul>

    </div>

    <div id="actions-footer">
        <a id="menu" class="icon" href="#" onclick="$('#nav-footer').toggle();return false;"><i class="fas fa-bars fa-lg" aria-hidden="true"></i> 菜单</a>
        <a id="toc" class="icon" href="#" onclick="$('#toc-footer').toggle();return false;"><i class="fas fa-list fa-lg" aria-hidden="true"></i> 目录</a>
        <a id="share" class="icon" href="#" onclick="$('#share-footer').toggle();return false;"><i class="fas fa-share-alt fa-lg" aria-hidden="true"></i> 分享</a>
        <a id="top" style="display:none" class="icon" href="#" onclick="$('html, body').animate({ scrollTop: 0 }, 'fast');"><i class="fas fa-chevron-up fa-lg" aria-hidden="true"></i> 返回顶部</a>
    </div>

  </div>
</div>

        
        <footer id="footer">
  <div class="footer-left">
    Copyright &copy; 2020 Dikang Li
  </div>
  <div class="footer-right">
    <nav>
      <ul>
         
          <li><a href="/">首页</a></li>
         
          <li><a href="/archives/">归档</a></li>
         
          <li><a href="/search/">搜索</a></li>
         
          <li><a href="/tags/">标签</a></li>
         
          <li><a href="/others/">其他文章</a></li>
        
      </ul>
    </nav>
  </div>
</footer>

    </div>
</body>
</html>
<!-- styles -->
<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
<link rel="stylesheet" href="/lib/justified-gallery/css/justifiedGallery.min.css">
<link rel="stylesheet" href="/lib/fancybox/jquery.fancybox.css">

<link rel="stylesheet" href="https://file.lantingshucheng.com/blog/v3/static/styles/gitalk.css">


<!-- jquery -->
<script src="/lib/jquery/jquery.min.js"></script>
<script src="/lib/justified-gallery/js/jquery.justifiedGallery.min.js"></script>
<script src="/lib/fancybox/jquery.fancybox.pack.js"></script>

<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/md5.js"></script>
<script type="text/javascript" src="https://file.lantingshucheng.com/blog/v3/static/scripts/gitalk.min.js"></script>

<script src="/js/main.js"></script>
<!-- search -->

<!-- Google Analytics -->

<!-- Baidu Analytics -->

    <script type="text/javascript">
        var _hmt = _hmt || [];
        (function() {
            var hm = document.createElement("script");
            hm.src = "https://hm.baidu.com/hm.js?7851ca7a86be957be994acb77639a5be";
            var s = document.getElementsByTagName("script")[0];
            s.parentNode.insertBefore(hm, s);
        })();
    </script>

<!-- Disqus Comments -->


  <script type="text/javascript">
    window.onload = function(){
      if (window.location.href.indexOf('andyliwr.github.io') > -1) {
        var gitalk = new Gitalk({
          clientID: 'ad6f70df0c70d2b715f9',
          clientSecret: '40384bee244c1e17dd7c37733d21e9ef669e969c',
          repo: 'Andyliwr.github.io',
          owner: 'Andyliwr',
          admin: ['Andyliwr'],
          id: md5(location.pathname),
          distractionFreeMode: 'true'
        })
        gitalk.render('gitalk_thread')
      }
    }
  </script>


